﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Reflection;

using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.FileSystem;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Communications;

using CharityGamesOnline.Modules.Common;

namespace CharityGamesOnline.Modules.Charity
{
    public partial class CharityNav : CGOBase, IModuleCommunicator
    {
        public event ModuleCommunicationEventHandler ModuleCommunication;
        protected int charityID = 0;
        public const string COMM_TEXT = "CharityID";
        public const string COMM_TYPE = "CharitySelectChange";        
        
        public bool boolViewArchive
        {
            get
            {
                if ((Request.QueryString["ViewArchive"] != null))
                {
                    return Boolean.Parse(Request.QueryString["ViewArchive"]);
                }
                else
                    return false;
            }
        }

        public int CharityID
        {
            get
            {
                if (charityID == 0)
                {
                    if ((Request.QueryString["CharityID"] != null))
                    {
                        charityID = Int32.Parse(Request.QueryString["CharityID"]);
                    }
                }
                return charityID;
            }
        }

        public bool EnablePartialRendering
        {
            get
            {
                if (!Null.IsNull(Settings["EnablePartialRendering"]))
                    return Boolean.Parse(Settings["EnablePartialRendering"].ToString());
                else
                    return false;
            }
        }

        public string CharityNameFormat
        {
            get
            {
                return "{0}...";
                //return Localization.GetString("CharityNameFormat", LocalResourceFile);
            }
        }

        #region Event Handler
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {                
                
                //if page is first load, check charity id url parameter first, if there is, use it
                //if not bind data directly, CharityID will be assigned with the id of the first charity in return list
                //if no charity return, CharityID remains the null value, -1
                //at last, if partial render is enabled, send communication to other module, if not, use redirect to nav
                if (!Page.IsPostBack)
                {                    
                    BindData();                    
                }
            }

            catch (Exception exc)
            {                
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        protected override void EnsureChildControls()
        {
            if (EnablePartialRendering && !Page.IsPostBack)
            {
                CommunicationSend(COMM_TYPE, COMM_TEXT, CharityID.ToString());
            }
            base.EnsureChildControls();            
        }
                
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void dlCharity_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
        {
            
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void dlCharity_ItemCreated(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
        {
            LinkButton btnCharityLink = e.Item.FindControl("btnCharityLink") as LinkButton;
            HyperLink lnkCharity = e.Item.FindControl("lnkCharity") as HyperLink;

            if (btnCharityLink != null)
                btnCharityLink.Visible = EnablePartialRendering;

            if (lnkCharity != null)
                lnkCharity.Visible = !EnablePartialRendering;

            if (EnablePartialRendering)
            {
                if (btnCharityLink != null)
                {
                    btnCharityLink.Click += new EventHandler(btnCharityLink_Click);
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnCharityLink_Click(object sender, EventArgs e)
        {
            LinkButton btnCharityLink = sender as LinkButton;
            if (btnCharityLink != null)
            {
                try
                {
                    CommunicationSend(COMM_TYPE, COMM_TEXT, btnCharityLink.CommandArgument);
                    charityID = Int32.Parse(btnCharityLink.CommandArgument);
                    BindData();
                }
                catch (Exception exc) //Module failed to load
                {
                    Exceptions.ProcessModuleLoadException(this, exc);
                }
            }
        }
        #endregion

        #region Private Funtions

        private void BindData()
        {
            CharityController ctrlCharity = new CharityController();
            List<CharityInfo> lstCharity;

            lstCharity = ctrlCharity.GetCharities(PortalId);

            if (CharityID==0)
            {
                if (lstCharity.Count > 0)
                {
                    charityID = lstCharity[0].CharityID;
                    Response.Redirect(Globals.NavigateURL(TabId, string.Empty, "CharityID=" + charityID.ToString()), true);
                }
                dlCharity.SelectedIndex = 0;
            }
            else
            {
                for (int i = 0; i < lstCharity.Count; i++)
                {
                    if (lstCharity[i].CharityID == CharityID)
                    {
                        dlCharity.SelectedIndex = i;
                        break;
                    }
                }
            }
            

            dlCharity.DataSource = lstCharity;
            dlCharity.DataBind();                        
        }

        private void CommunicationSend(string Type, string Text, string Value)
        {
            ModuleCommunicationEventArgs MCArgs = new ModuleCommunicationEventArgs();
            MCArgs.Sender = "CharityGamesOnline.Modules.Charity.CharityNav";
            MCArgs.Type = Type;
            MCArgs.Target = string.Empty;
            MCArgs.Text = Text;
            MCArgs.Value = Value;

            if (ModuleCommunication != null)
            {
                ModuleCommunication(this, MCArgs);
            }
        }
        
        #endregion
    }
}